<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/10
 * Time: 13:25
 */
namespace Pay\Controller;

use Think\Controller;

class OrderController extends Controller{
    public function queryOrder(){
        $orderid = I("request.orderid","");
        $pay_memberid = I("request.pay_memberid",0,"intval");
        $pay_md5sign = I("request.pay_md5sign","");


        $ret = array("status"=>-1, "msg"=>"");

        $order = M("Order")->where(array("out_trade_id"=>$orderid))->find();

        if(!$order){
            $ret['msg'] = '未找到此订单';
            $this->ajaxReturn($ret);
        }

        $member = M("Member")->where(array("id"=>getIdByBianHao($pay_memberid)))->find();
        if(!$member){
            $ret['msg'] = '商户不能存在';
            $this->ajaxReturn($ret);
        }

        if($order['pay_memberid'] != $pay_memberid){
            $ret['msg'] = '订单不存在';
            $this->ajaxReturn($ret);
        }

        if($this->sign($orderid, $member['apikey']) !== $pay_md5sign){
            $ret['msg'] = '签名错误';
            $this->ajaxReturn($ret);
        }

        switch($order['pay_status']){
            case 0:
                $ret['status'] = 0;
                $ret['msg'] = "未支付";
                break;
            case 1:
                $ret['status'] = 1;
                $ret['msg'] = '成功，未返回';
                break;
            default:
                $ret['status'] = 2;
                $ret['msg'] = '成功，已返回';
        }

        $this->ajaxReturn($ret);
    }

    public function sign($orderid, $key){
        return md5($orderid . $key);
    }
}